package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dtos.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ghy
 * @version v1.0
 * create on 2022/11/20 9:06
 */
@RestController
@RequestMapping("/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 根据分类ID查询
     * @param dish
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据分类ID查询菜品")
    public R list(Dish dish){
        return dishService.findListAndFlavors(dish);
    }


    /**
     * 更新
     * @param dishDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto){
        return dishService.updateWithFlavors(dishDto);
    }

    /**
     * 根据ID查询菜品和关联口味
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R page(@PathVariable("id") Long id){
        return dishService.findByIdWithFlavors(id);
    }

    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页条件查询菜品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页面", example = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", example = "10", required = true),
            @ApiImplicitParam(name = "name", value = "菜品名称", example = "豆腐", required = false)
    })
    public R page(Integer page, Integer pageSize, String name){
        return dishService.findPage2(page, pageSize, name);
    }

    /**
     * 保存
     * @param dishDto 菜品数据 和 品味数据
     * @return
     */
    @PostMapping
    public R add(@RequestBody DishDto dishDto) {
       return dishService.add(dishDto);
    }

}
